From 413e7f19546f9730930e69b0bb3cc07651cb0a59 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Mon, 9 Jun 2014 02:30:38 +0200 Subject: [PATCH] add internal babl_parse_double; to avoid using atof atof is locale dependent; it was being used two places parsing floating point values from environment variables. --- babl/babl-fish-path.c | 2 +- babl/babl-fish-stats.c | 2 +- babl/babl-internal.h | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/babl/babl-fish-path.c b/babl/babl-fish-path.c index 3b90edf..5dc0601 100644 --- a/babl/babl-fish-path.c +++ b/babl/babl-fish-path.c @@ -109,7 +109,7 @@ static double legal_error (void) env = getenv ("BABL_TOLERANCE"); if (env && env[0] != '\0') - error = atof (env); + error = babl_parse_double (env); else error = BABL_LEGAL_ERROR; return error; diff --git a/babl/babl-fish-stats.c b/babl/babl-fish-stats.c index 9c6d52e..c0b34de 100644 --- a/babl/babl-fish-stats.c +++ b/babl/babl-fish-stats.c @@ -252,7 +252,7 @@ static double legal_error (void) env = getenv ("BABL_TOLERANCE"); if (env && env[0] != '\0') - error = atof (env); + error = babl_parse_double (env); else error = BABL_LEGAL_ERROR; return error; diff --git a/babl/babl-internal.h b/babl/babl-internal.h index cc05660..eb80dd0 100644 --- a/babl/babl-internal.h +++ b/babl/babl-internal.h @@ -295,4 +295,26 @@ babl_##klass##_from_id (int id) \ #define BABL(obj) ((Babl*)(obj)) +static inline double babl_parse_double (const char *str) +{ + double result = 0; + if (!str) + return 0.0; + result = atoi (str); + if (strchr (str, '.')) + { + char *p = strchr (str, '.') + 1; + double d = 10; + for (;*p && *p > '0' && *p < '9';p++, d *= 10) + { + if (result >= 0) + result += (*p - '0') / d; + else + result -= (*p - '0') / d; + } + } + return result; +} + + #endif -- 2.30.2